package com.tyk.bookstore.front.member.controller;


import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.member.model.query.DeliveryAddressAddQuery;
import com.tyk.bookstore.front.member.model.query.DeliveryAddressUpdateQuery;
import com.tyk.bookstore.front.member.model.vo.DeliveryAddressVo;
import com.tyk.bookstore.front.member.service.DeliveryAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author tyk
 * @description 收获地址管理
 */
@Tag(name = "会员管理/收获地址管理", description = "收获地址管理")
@RequestMapping({"api/member/address"})
@RequiredArgsConstructor
@RestController
public class DeliveryAddressController {
    private final DeliveryAddressService deliveryAddressService;

    @GetMapping({"{id}"})
    @Operation(summary = "获取送货地址详情", description = "获取送货地址详情")
    public R<DeliveryAddressVo> getById(
            @Schema(description = "收货地址ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(deliveryAddressService.getById(id));
    }

    @GetMapping({"list"})
    @Operation(summary = "查询送货地址列表", description = "查询送货地址列表")
    public R<List<DeliveryAddressVo>> getList() {
        return R.ok(deliveryAddressService.getList());
    }

    @PostMapping
    @Operation(summary = "添加送货地址", description = "添加送货地址")
    public R<Object> add(@Validated @RequestBody DeliveryAddressAddQuery query) {
        deliveryAddressService.add(query);
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "更新送货地址", description = "更新送货地址")
    public R<Object> updateById(@Validated @RequestBody DeliveryAddressUpdateQuery query) {
        deliveryAddressService.updateById(query);
        return R.ok();
    }

    @PutMapping("default/{id}")
    @Operation(summary = "设置默认地址", description = "设置默认地址")
    public R<Object> setDefault(
            @NotNull @Schema(description = "收货地址ID") @PathVariable Long id
    ) {
        deliveryAddressService.setDefault(id);
        return R.ok();
    }

    @DeleteMapping({"{id}"})
    @Operation(summary = "删除送货地址", description = "删除送货地址")
    public R<Object> deleteById(
            @Schema(description = "收货地址ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        deliveryAddressService.deleteById(id);
        return R.ok();
    }
}
